package com.cloudxingkong.dianyinke.Student.Ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Adapter.CourseBookingAdapter;
import com.cloudxingkong.dianyinke.Student.Fragment.TeacherDynamicFragment;
import com.cloudxingkong.dianyinke.Student.Fragment.TeacherPtitudeFragment;
import com.cloudxingkong.dianyinke.Student.bean.CourseBookingEntity;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 学生端 老师个人资料
 *
 * @Zxrui
 */
public class CourseBooking extends BaseActivity {
    private List<Fragment> listFragment;
    private List<String> listTitle;
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private TeacherPtitudeFragment teacheraPtitude;
    private CourseBookingAdapter courseBookingAdapter;
    private SharedPreferences sharedPreferences;
    private static final String getCB = ThisUri.SERVER_YUMING + StudentUri.COURSEBOOKING;
    private String sessionid, teacherID;
    private TextView cb_name, cb_jingyan, cb_signature, tvIdPic, tvProPic, tvTefPic;
    private ProgressDialog progressDialog;
    private boolean isQry = false;
    private CircleImageView cbAvatar;

    private String dyPresentjob;
    private String dyGlory;
    private String dyEpc;
    private Fragment instance;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setContentView(R.layout.activity_course_booking);
        init();
        //Tab初始化
        qryTeacher();

    }

    private void initTab() {
        tabLayout = (TabLayout) findViewById(R.id.tb_tabLayout);
        viewPager = (ViewPager) findViewById(R.id.vp_yueKe);

        listFragment = new ArrayList<>();
        listTitle = new ArrayList<>();

        instance = TeacherDynamicFragment.getInstance(dyEpc, dyGlory, dyPresentjob);

        teacheraPtitude = TeacherPtitudeFragment.getInstance(teacherID, sessionid);

        listFragment.add(instance);
        listFragment.add(teacheraPtitude);

        listTitle.add("老师资料");
        listTitle.add("老师动态");

        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.addTab(tabLayout.newTab().setText(listTitle.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText(listTitle.get(1)));
        courseBookingAdapter = new CourseBookingAdapter(getSupportFragmentManager(), listFragment, listTitle);
        viewPager.setAdapter(courseBookingAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void qryTeacher() {
        sharedPreferences = this.getSharedPreferences("data_dyk", 0);
        sessionid = CookieHelper.getSessionID(sharedPreferences);

        progressDialog = ProgressDialog.show(this, null, "正在获取老师资料...", true, false);
        //获取老师资料
        final RequestParams params = new RequestParams(ThisUri.SERVER_YUMING + StudentUri.COURSEBOOKING);
        params.addHeader("cookie", sessionid);
        params.addBodyParameter("teacherid", teacherID + "");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("test", "老师个人资料" + result);
                //  Object success =  LoginJsonUtils.getJsonFromLogin(result).get("success");
                if (result != null && !equals("")) {
                    Message msg = handler.obtainMessage();
                    msg.what = 1;
                    msg.obj = result;
                    handler.sendMessage(msg);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                progressDialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private Handler handler = new Handler() {


        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    String state = (String) msg.obj;
                    if (state != null && !state.equals("")) {
                        Gson gson = new Gson();
                        CourseBookingEntity entity = null;
                        try {

                            entity = gson.fromJson(state, CourseBookingEntity.class);
                        } catch (Exception e) {
                            Log.e("test", "老师资料 " + e.getMessage());
                        }
                        isQry = entity.isSuccess();
                        if (isQry == true) {
                            CourseBookingEntity.ObjBean obj = entity.getObj();
                            if (obj.getIdenpic() != null && !obj.getIdenpic().equals("")) {
                                tvIdPic.setVisibility(View.VISIBLE);
                            }
                            if (obj.getPfcfpic() != null && !obj.getPfcfpic().equals("")) {
                                tvProPic.setVisibility(View.VISIBLE);
                            }
                            if (obj.getTeflpic() != null && !obj.getTeflpic().equals("")) {
                                tvTefPic.setVisibility(View.VISIBLE);
                            }
                            cb_signature.setText(obj.getSignature());
                            //教龄
                            String avatar = obj.getCoverpic();
                            if (avatar != null && !avatar.equals("")) {
                                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(avatar, cbAvatar);
                            }
                            dyEpc = obj.getWorkepc();
                            dyGlory = obj.getGlory();
                            dyPresentjob = obj.getPresentjob();
                            initTab();
                            progressDialog.dismiss();
                        }
                        break;

                    }

            }
        }
    };

    private void init() {
        cb_name = (TextView) findViewById(R.id.cb_name);
        cb_jingyan = (TextView) findViewById(R.id.cb_jingyan);
        cb_signature = (TextView) findViewById(R.id.cb_signature);
        tvIdPic = (TextView) findViewById(R.id.cb_pic_idcard);
        tvProPic = (TextView) findViewById(R.id.cb_pic_pro);
        tvTefPic = (TextView) findViewById(R.id.cb_pic_tef);
        cbAvatar = (CircleImageView) findViewById(R.id.cb_avatar);
        Intent intent = getIntent();
        teacherID = intent.getStringExtra("teacherID");
        Log.e("test", "teacherID " + teacherID);
        String tMemname = intent.getStringExtra("tMemname");
        String pic = intent.getStringExtra("teacherPIC");
        if (pic != null) {
            ImageLoader.getInstance().displayImage(pic, cbAvatar);
        }
        cb_name.setText(tMemname);


    }

    //约课
    public void yoke(View view) {
        Intent intent = new Intent(this, SelectCurricula.class);
        intent.putExtra("teacherID", teacherID);
        startActivity(intent);
    }

    public void back(View view) {
        finish();
    }

}
